/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.feiyue.basic.server.parser.Chapter;
import java.util.ArrayList;
import java.util.List;

public class ChapterDBHandler {
	public static final String COLUMN_bookid = "bookid";
	public static final String COLUMN_categoryid = "categoryid";
	public static final String COLUMN_id = "id";
	public static final String COLUMN_name = "name";
	public static final String CREATE_TABLE_chapterlist;
	public static String myTableName = "chapterlist";
	public static String suffix = "_%d";
	protected SQLiteDatabase mSQLiteDatabase = null;

	static {
		CREATE_TABLE_chapterlist = "CREATE TABLE IF NOT EXISTS "
				+ myTableName
				+ suffix
				+ " (categoryid INTEGER not null, bookid INTEGER not null, id INTEGER PRIMARY KEY not null, name text not null);";
	}

	public ChapterDBHandler(Context paramContext) throws Exception {
		this.mSQLiteDatabase = paramContext.openOrCreateDatabase("novel", 0,
				null);
	}

	public boolean IsTableExist(String paramString) {
		if (paramString == null)
			return false;
		Cursor localCursor = null;
		try {
			String str = "select count(*) as c from Sqlite_master  where type ='table' and name ='"
					+ paramString.trim() + "' ";
			localCursor = this.mSQLiteDatabase.rawQuery(str, null);
			if (localCursor != null && localCursor.moveToNext()) {
				int j = localCursor.getInt(0);
				if (j > 0) {
					return true;
				}
			}
		} catch (Exception localException) {
			localException.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		
		return false;
	}

	public void delete(int i)
    {
        StringBuilder stringbuilder = new StringBuilder(String.valueOf(myTableName));
        String s = suffix;
        Object aobj[] = new Object[1];
        aobj[0] = Integer.valueOf(i);
        String s1 = stringbuilder.append(String.format(s, aobj)).toString();
        if (IsTableExist(s1))
        {
            String s2 = (new StringBuilder("drop table ")).append(s1).toString();
            mSQLiteDatabase.execSQL(s2);
        }
    }

	public void destroy()
    {
        if (mSQLiteDatabase != null)
            mSQLiteDatabase.close();
    }

	public List<Chapter> getChapterList(Context paramContext, int paramInt1,
			int paramInt2) {
		StringBuilder localStringBuilder = new StringBuilder(
				String.valueOf(myTableName));
		String str1 = suffix;
		Object[] arrayOfObject = new Object[1];
		arrayOfObject[0] = Integer.valueOf(paramInt2);
		String str2 = localStringBuilder.append(String.format(str1, arrayOfObject)).toString();
		if (!(IsTableExist(str2)))
			return null;
		ArrayList localArrayList = new ArrayList();
		String str3 = "select * from " + str2;
		
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(str3, null);
			if (localCursor != null && localCursor.moveToFirst()) {
			    do {
                    Chapter localChapter = new Chapter();
                    String str4 = localCursor.getString(localCursor
                            .getColumnIndex("name"));
                    int i = localCursor
                            .getInt(localCursor.getColumnIndex("id"));
                    localChapter.setName(str4);
                    localChapter.setId(i);
                    localChapter.setCategoryId(paramInt1);
                    localChapter.setBookId(paramInt2);
                    localArrayList.add(localChapter);
                } while (localCursor.moveToNext());
			}
				
			return localArrayList;
		} catch (Exception localException) {
			localException.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		return null;
	}

	public void insert(Chapter chapter, Context context)
    {
        StringBuilder stringbuilder = new StringBuilder(String.valueOf(myTableName));
        String s = suffix;
        Object aobj[] = new Object[1];
        aobj[0] = Integer.valueOf(chapter.getBookId());
        String s1 = stringbuilder.append(String.format(s, aobj)).toString();
        String s2 = (new StringBuilder("insert into ")).append(s1).append(" values(").append(chapter.getCategoryId()).append(", ").append(chapter.getBookId()).append(", ").append(chapter.getId()).append(", \"").append(chapter.getName().replace("\"", "")).append("\"").append(");").toString();
        mSQLiteDatabase.execSQL(s2);
    }

	public void insert(List list, Context context)
    {
        if (list != null && list.size() != 0) {
            StringBuilder stringbuilder = new StringBuilder(String.valueOf(myTableName));
            String s = suffix;
            Object aobj[] = new Object[1];
            aobj[0] = Integer.valueOf(((Chapter)list.get(0)).getBookId());
            if (!IsTableExist(stringbuilder.append(String.format(s, aobj)).toString()))
            {
                SQLiteDatabase sqlitedatabase = mSQLiteDatabase;
                String s1 = CREATE_TABLE_chapterlist;
                Object aobj1[] = new Object[1];
                aobj1[0] = Integer.valueOf(((Chapter)list.get(0)).getBookId());
                sqlitedatabase.execSQL(String.format(s1, aobj1));
            }
            int i = list.size();
            mSQLiteDatabase.beginTransaction();
            int j = 0;
            
            while (j < i) {
                insert((Chapter)list.get(j), context);
                j++;
            }
            
            mSQLiteDatabase.setTransactionSuccessful();
            mSQLiteDatabase.endTransaction();
        }
    }

	public boolean isExist(Chapter paramChapter) {
		StringBuilder localStringBuilder = new StringBuilder(
				String.valueOf(myTableName));
		String str1 = suffix;
		Object[] arrayOfObject1 = new Object[1];
		arrayOfObject1[0] = Integer.valueOf(paramChapter.getBookId());
		String str2 = localStringBuilder.append(String.format(str1, arrayOfObject1)).toString();
		if (IsTableExist(str2)) {
			String str4 = "select * from " + str2 + " where id = "
					+ paramChapter.getId();
			Cursor localCursor = null;
			try {
				localCursor = this.mSQLiteDatabase.rawQuery(str4, null);
				if (localCursor != null && localCursor.moveToFirst()) {
					return true;
				}
			} catch (Exception localException) {
				localException.printStackTrace();
				return false;
			} finally {
				if (localCursor != null) {
					localCursor.close();
				}
			}
			return false;
		}
		SQLiteDatabase localSQLiteDatabase = this.mSQLiteDatabase;
		String str3 = CREATE_TABLE_chapterlist;
		Object[] arrayOfObject2 = new Object[1];
		arrayOfObject2[0] = Integer.valueOf(paramChapter.getBookId());
		localSQLiteDatabase.execSQL(String.format(str3, arrayOfObject2));
		return false;
	}
}